<%@page import="jobportal.Dbconn"%>
<%@page import="javax.imageio.ImageIO"%>
<%@page import="java.awt.image.BufferedImage"%>

<%@ page import="java.sql.*,java.io.*" %>
<%

            if (request.getParameter("id") != null) {
                String id = request.getParameter("id");
                String table = request.getParameter("table");
                String col = request.getParameter("col");
                String pk_col = request.getParameter("pk_col");
                String sql = "Select " + col + " From " + table + " Where " + pk_col + " = '" + id + "' and " + col + " is not null";
                try {
                    Connection con = Dbconn.getConn();
                    Statement st = con.createStatement();

                    Blob img;

                    ResultSet rs = st.executeQuery(sql);
                    byte[] imgData = new byte[10000];
                    if (rs.next()) {
                        img = rs.getBlob(1);
                        imgData = img.getBytes(1, (int) img.length());
                    } else {
                        String s = "";
                        if (table.equalsIgnoreCase("student")) {
                            rs = st.executeQuery("select gender from student where username='" + id + "'");
                            if (rs.next()) {
                                if (rs.getString("gender").equalsIgnoreCase("male")) {
                                    s = request.getRealPath("/images/stuimgm.gif");
                                } else {
                                    s = request.getRealPath("/images/stuimgf.gif");
                                }
                            } else {
                                s = request.getRealPath("/images/photonotavlbl.jpg");
                            }
                        } else {
                            s = request.getRealPath("/images/photonotavlbl.jpg");
                        }
                        out.println(s);
                        BufferedImage originalImage = ImageIO.read(new File(s));
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        ImageIO.write(originalImage, "jpg", baos);
                        imgData = baos.toByteArray();
                    }
                    response.setContentType("image/gif");
                    OutputStream o = response.getOutputStream();
                    o.write(imgData);
                    o.flush();
                    o.close();
                    st.close();
                    con.close();

                } catch (Exception e) {
                    
                    out.println("Exception::" + e);
                }
            } else {
                out.println("Invalid Data");
            }
%>
